//
//  LoveStuViewController.m
//  hellomui
//
//  Created by zhangtao on 15/9/23.
//  Copyright © 2015年 zt.td. All rights reserved.
//

#import "LoveStuViewController.h"
#import "LLListenViewController.h"
//#import "LLLookViewController.h"
#import "DDifficultViewController.h"
#import "LoginAndRegistViewController.h"
#import "ScanViewController.h"
@interface LoveStuViewController ()<UIWebViewDelegate>
@property (strong, nonatomic) IBOutlet UIButton *loadBtn;
@property (strong, nonatomic) IBOutlet UIView *noNetStatusIV;
@property (strong, nonatomic) IBOutlet UIWebView *web;
//@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *loading;//正在加载---动画
@property(nonatomic,strong)NSURLRequest *url;
//@property(nonatomic,assign)BOOL *isLogin;
@end

@implementation LoveStuViewController


{
    BOOL isLogin;
}




-(void)testLL:(NSString *)str{
    
    
    if (!NSUSER_DEF(@"USERID")) {
        LoginAndRegistViewController *login = [self.storyboard instantiateViewControllerWithIdentifier:@"loginAndRegist"];
        [self presentViewController:login animated:NO completion:nil];
    }else{
        LLListenViewController *lll = [[LLListenViewController alloc]initWithNibName:@"LLListenViewController" bundle:nil];
        
        lll.idValue = str;
        [self presentViewController:lll
                           animated:YES completion:nil];

    }

    
//    UIViewController *lll = [self.storyboard instantiateViewControllerWithIdentifier:@"test"];
    
}

//video
-(void)testLV:(NSString *)str{
     if (!NSUSER_DEF(@"USERID")) {
         LoginAndRegistViewController *login = [self.storyboard instantiateViewControllerWithIdentifier:@"loginAndRegist"];
         [self presentViewController:login animated:NO completion:nil];

     }else{
         DDifficultViewController *difi = [[DDifficultViewController alloc]initWithNibName:@"DDifficultViewController" bundle:nil];
         
         difi.idValue = str;
         [[MBProgressController sharedInstance]hide];
         //    [self presentViewController:difi animated:YES completion:nil];
         [difi setHidesBottomBarWhenPushed:YES];
         [self.navigationController pushViewController:difi animated:YES];
        
     }
}


- (void)viewDidLoad {
    [super viewDidLoad];
    isLogin = YES;
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor, [UIFont systemFontOfSize:20.0],UITextAttributeFont,nil]];

//    [self loadingAnimated];
    _web.delegate = self;
//
    [self netStatus];
    self.loadBtn.layer.borderWidth = 2;
    self.loadBtn.layer.borderColor = [[UIColor blueColor]CGColor];
         [_web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:HOME_PAGE_WEB]]];
       [self inits];
}

-(void)inits{
    NSString *status = [[NSUserDefaults standardUserDefaults]valueForKey:@"netStatus"];
    if ([status isEqualToString:@"0"] ||
        [status isEqualToString:@"-1"]) {
         _web.hidden = YES;
        
        self.loadBtn.layer.borderWidth = 2;
        self.loadBtn.layer.borderColor = [[UIColor colorWithRed:253/255.f green:183/255.f blue:180/255.f alpha:1]CGColor];
        _noNetStatusIV.hidden = _loadBtn.hidden = NO;
        
//        _loading.hidden = YES;
       
    }else{
        _noNetStatusIV.hidden = _loadBtn.hidden = YES;
        
        _web.hidden = NO;
    }
    

}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self netStatus];
    self.tabBarController.tabBar.hidden = NO;
    self.navigationController.navigationBar.hidden = YES;
    
    if (self.webUrl) {
        [_web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:_webUrl]]];
    }
//    else{
//        [_web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:HOME_PAGE_WEB]]];
//    }

}

-(void)netStatus{
    NSString *status = [[NSUserDefaults standardUserDefaults]valueForKey:@"netStatus"];
    if ([status isEqualToString:@"0"] ||
        [status isEqualToString:@"-1"]) {
        _noNetStatusIV.hidden = NO;
    }else{
        _noNetStatusIV.hidden = YES;
    }
}

- (IBAction)loadBtn:(id)sender {
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    
    BOOL bools = YES;
    NSLog(@":::%@",request);
    NSString *str = [NSString stringWithFormat:@"%@",request];
    if ([str rangeOfString:@"bookId=" options:NSRegularExpressionSearch].location != NSNotFound) {
        _url = request;
    }
   
//    NSRange range = [str rangeOfString:@"bookTextId=" options:NSRegularExpressionSearch];
    if ([str rangeOfString:@"bookTextId=" options:NSRegularExpressionSearch].location != NSNotFound) {

        NSArray *array = [str componentsSeparatedByString:@"bookTextId="];
        
        NSString *value = [NSString stringWithFormat:@"%@",[array[1] componentsSeparatedByString:@" }"][0]];
        
        NSLog(@"hehe~~~%@",value);
        bools = NO;
        
        [self testLL:value];
    }else if ([str rangeOfString:@"videoId=" options:NSRegularExpressionSearch].location != NSNotFound){
        [[MBProgressController sharedInstance] showWithText:@"正在加载"];

        NSArray *array = [str componentsSeparatedByString:@"videoId="];
        NSString *value = [NSString stringWithFormat:@"%@", [array[1] componentsSeparatedByString:@" }"][0]];
        bools = NO;
        [self testLV:value];
    }else if ([str rangeOfString:@"bracodeScan" options:NSRegularExpressionSearch].location != NSNotFound) {
        NSLog(@"扫描");
        ScanViewController *scan = [self.storyboard instantiateViewControllerWithIdentifier:@"scan"];
        scan.loveStu = self;
        [self presentViewController:scan animated:NO completion:nil];
        bools = NO;
    }
     // 同步教辅
    else if([str rangeOfString:@"switchLogin" options:NSRegularExpressionSearch].location != NSNotFound){
        if (NSUSER_DEF(@"USERID")) {
            [ToolObjc loginLoginIn:[NSString stringWithFormat:@"%@",NSUSER_DEF(@"USERID")]];
//            bools = YES;
        }else{
            bools = false;
                [_web loadRequest:_url];
                _url = nil;
                LoginAndRegistViewController *login = [self.storyboard instantiateViewControllerWithIdentifier:@"loginAndRegist"];
                [self presentViewController:login animated:YES completion:nil];
             bools = NO;
        }
        
    }
//
    return bools;
    
//    return bools;


}



- (void)webViewDidStartLoad:(UIWebView *)webView{
    NSLog(@"webViewDidStartLoad");
//    _loading.hidden = NO;
//    [_loading setCenter:CGPointMake(MAIN_WIDTH/2+30,MAIN_HEIGHT/2)];
//    _loading.backgroundColor = [UIColor grayColor];
//    [_loading setTintColor:[UIColor grayColor]];
//    _loading.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
//    [self.view addSubview:_loading];
//    [_loading startAnimating];
    if (_isScan) {
        _isScan = NO;

    }else{
    [[MBProgressController sharedInstance]showWithText:@"正在加载"];
    }
    
    
}
- (void)webViewDidFinishLoad:(UIWebView *)web{
    [[MBProgressController sharedInstance]hide];

    NSLog(@"webViewDidFinishLoad");
    self.navigationItem.title=[web stringByEvaluatingJavaScriptFromString:@"document.title"];//获取当前页面的title
//    [_loading stopAnimating];
//    _loading.hidden = YES;
    
//    _web.backgroundColor = [UIColor blackColor];
//    [(UIScrollView *)[[_web subviews] objectAtIndex:0] setBounces:NO];
      [(UIScrollView *)[[_web subviews] objectAtIndex:0] setBounces:NO];
    
    
    
//    return [self webViewDidFinishLoad:_web];
}

-(void)loadingAnimated{
    //    _loading.frame = CGRectMake(MAIN_WIDTH/2-30, MAIN_HEIGHT/2-30, 60, 60);
    

}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
